/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gamesync;

import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

 /*
 *
 * @author trungdung
 */
public class Encrypter {

	private Cipher ecipher;
	private Cipher dcipher;

	//private static SecretKey key;
        private String key="Kjfk]!k3"; //key gom 8 ky tu
        SecretKey skey = new SecretKeySpec(key.getBytes(), 0, key.length(), "DES");

	public String encrypt(String str) {

            try {
                  // encode the string into a sequence of bytes using the named charset
                  // storing the result into a new byte array.
                    ecipher = Cipher.getInstance("DES");
                    ecipher.init(Cipher.ENCRYPT_MODE, skey);
                    byte[] utf8 = str.getBytes("UTF8");
                    byte[] enc = ecipher.doFinal(utf8);
                    // encode to base64
                    enc = BASE64EncoderStream.encode(enc);
                    return new String(enc);
            }
            catch (Exception e) {
            }
            
            return null;
        }

	public String decrypt(String str) {

            try {
                  // decode with base64 to get bytes
                dcipher = Cipher.getInstance("DES");
                dcipher.init(Cipher.DECRYPT_MODE, skey);
                byte[] dec = BASE64DecoderStream.decode(str.getBytes());
                byte[] utf8 = dcipher.doFinal(dec);
                // create new string based on the specified charset
                return new String(utf8, "UTF8");
            }
            catch (Exception e) {
                  e.printStackTrace();
            }
            return null;
    }

}
